package ar.edu.caece.compiler.grammar.attributes.tree;

import java.util.ArrayList;
import java.util.List;

public class NodeMostrar extends NodeSentencia {

	private List<NodeExpr> aMostrar;
	private boolean isMostrarLN;

	public NodeMostrar() {
		aMostrar = new ArrayList<NodeExpr>();
		isMostrarLN = false;
	}
	
	public String generateDeclaration() {
		StringBuilder builder = new StringBuilder();
		
		for(NodeExpr nodo : aMostrar) {
			builder.append(nodo.generateDeclaration()).append(System.getProperty("line.separator"));
		}
		
		return builder.toString();
	}
	
	public NodeMostrar(boolean isMostrarLN) {
		this();
		this.isMostrarLN = isMostrarLN;
	}
	
	public List<NodeExpr> getaMostrar() {
		return aMostrar;
	}

	public void setaMostrar(List<NodeExpr> aMostrar) {
		this.aMostrar = aMostrar;
	}
	
	public NodeMostrar(List<NodeExpr> aMostrar) {
		super();
		this.aMostrar = aMostrar;
	}
	
	public void addMostrarNode(NodeExpr nodo) {
		aMostrar.add(nodo);
	}
	@Override
	public String toString() {
		return "NodeMostrar [aMostrar=" + aMostrar + "]";
	}
	@Override
	public String generateCode() {
		StringBuilder builder = new StringBuilder();
		
		for(NodeExpr expr : aMostrar) {
		
			String signo = Type.ENTERO.equals(expr.getTipo())?"0001h":"0000h";
			
			builder.append("push ax").append(System.getProperty("line.separator"));
			if(!expr.isCadena()) builder.append(expr.generateCode()).append(System.getProperty("line.separator"))
							.append("push ").append(signo).append(System.getProperty("line.separator"))
							.append("push cx").append(System.getProperty("line.separator"));							
			
			else builder.append("push offset ").append(expr.generateCode()).append(System.getProperty("line.separator"))
						.append("push ").append(expr.getLongitud()).append(System.getProperty("line.separator"));
			
			builder.append("call ").append(expr.getWriteRoutineName()).append(System.getProperty("line.separator"));
			builder.append("pop ax").append(System.getProperty("line.separator"));
		}
		
		if(isMostrarLN) builder.append("call writeCRLF").append(System.getProperty("line.separator"));
		
		return builder.toString();
	}

	@Override
	public void propagarEntornoSuperior(NodeFuncProc nodeFuncProc) {
		for(NodeExpr nodo : aMostrar) {
			nodo.propagarEntornoSuperior(nodeFuncProc);
		}
	}
	
	@Override
	public void propagateRoot(NodeListaBloques raiz) {
		for(NodeExpr nodo : aMostrar) {
			nodo.propagateRoot(raiz);
		}
	}
	public void setMostrarLN(boolean isMostrarLN) {
		this.isMostrarLN = isMostrarLN;
	}
	public boolean isMostrarLN() {
		return isMostrarLN;
	}

}
